import type { SearchReportQueryKey } from '@/apis/report/types'
import { computed } from 'vue'
import { useRoute } from 'vue-router'

export function useRouteQuery() {
  const route = useRoute()
  const query = computed<SearchReportQueryKey[2]>(() => {
    const { query, params } = route

    const searchQuery: SearchReportQueryKey[2] = {
      classID: query.class === null ? undefined : +query.class,
      experimentID: query.experiment === null ? undefined : +query.experiment,
      status: query.status === null ? undefined : +query.status
    }

    if (query.student !== null) {
      searchQuery.student = Array.isArray(query.student)
        ? query.student[0] ?? undefined
        : query.student
    }

    if (route.params.classID !== undefined) {
      searchQuery.classID = +params.classID
    }
    if (params.experimentID !== undefined) {
      searchQuery.experimentID = +params.experimentID
    }

    return searchQuery
  })

  return {
    query
  }
}
